<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row>
        <el-col>
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单数据表格 -->
      <el-table :data="ordersList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag
              size="mini"
              type="success"
              v-if="scope.row.pay_status !== '0'"
              >已付款</el-tag
            >
            <el-tag size="mini" type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              title="修改订单地址"
              @click="editOrdersBtn"
            ></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              title="物流进度"
              @click="processBtn"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 修改按钮对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="editOrdersDialogVisible"
        width="50%"
        @close="editOrdersDialogClose"
      >
        <el-form
          :model="editOrdersForm"
          :rules="editOrdersRules"
          ref="editOrdersRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              v-model="editOrdersForm.address1"
              :options="citydata"
              :props="{ expandTrigger: 'hover' }"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="editOrdersForm.address"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editOrdersDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editOrdersDialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 物流进度对话框 -->
      <el-dialog
        title="物流进度"
        :visible.sync="processDialogVisible"
        width="50%"
      >
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in processInfo.data"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getOrdersListParams.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="getOrdersListParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import citydata from './citydata.js'
export default {
  created () {
    this.getOrdersList()
  },
  data () {
    return {
      // 获取订单数据的参数
      getOrdersListParams: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单数据列表
      ordersList: [],
      // 订单数据总条数
      total: 0,
      // 修改订单对话框开启和关闭
      editOrdersDialogVisible: false,
      // 修改订单表单
      editOrdersForm: {
        address1: [],
        address2: ''
      },
      // 修改订单验证规则
      editOrdersRules: {
        address1: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 省市区/县 列表
      citydata,
      // 物流进度对话框开启和关闭
      processDialogVisible: false,
      // 查询到的物流信息
      processInfo: ''
    }
  },
  methods: {
    // 获取订单数据列表
    async getOrdersList () {
      let { data: res } = await this.$http.get('orders', {
        params: this.getOrdersListParams
      })
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    // 分页每页显示数量改变事件
    handleSizeChange (size) {
      this.getOrdersListParams.pagesize = size
      this.getOrdersList()
    },
    // 分页当前页改变事件
    handleCurrentChange (page) {
      this.getOrdersListParams.pagenum = page
      this.getOrdersList()
    },
    // 点击修改按钮事件
    editOrdersBtn () {
      this.editOrdersDialogVisible = true
    },
    // 修改对话框关闭事件
    editOrdersDialogClose () {
      this.$refs.editOrdersRef.resetFields()
    },
    // 点击物流进度按钮
    async processBtn () {
      this.processDialogVisible = true
      let { data: res } = await this.$http.get('/kuaidi/test')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.processInfo = res.data
      console.log(this.processInfo)
    }
  }
}
</script>

<style lang="less" scoped>
@import "../../plugins/timeline/timeline.css";
@import "../../plugins/timeline-item/timeline-item.css";
.el-input-group {
  width: 35%;
}
.el-cascader {
  width: 100%;
}
</style>
